package com.cpkso.yzx.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cpkso.yzx.domain.Col;
import com.cpkso.yzx.domain.News;
import com.cpkso.yzx.repository.ColRepository;
import com.cpkso.yzx.repository.NewsRepository;

import runze.util.base.IService;
import runze.util.base.Response;
import runze.util.exception.DataNotGivenException;
import runze.util.io.UploadUtil;
import runze.util.property.Property;

/**
 * 栏目管理
 * 
 * 更新需要读
 * 
 * @author wrzhxy@qq.com
 * @date 2017年11月9日
 */
@Service
@Transactional
public class ColService implements IService<Col> {
	/**
	 * 前台
	 */
	/**
	 * 根据栏目找新闻
	 */
	public Response findByCol(String urlcode) {
		List<News> data = newsRepository.findByCol(urlcode);
		Response response = makeResponse();
		response.setData(data);
		return response;
	}
	public Response get(long id) {
		News data = newsRepository.get(id);
		Response response = makeResponse();
		response.setData(data);
		return response;
	}
	public Response list() {
		List<Col> data = colRepository.find();
		Response response = makeResponse();
		response.setData(data);
		return response;
	}
	
	/**
	 * 后台
	 */
	public long addCol(Col col) {
		return colRepository.add(col);
	}
	public void delCol(long id) {
		colRepository.del(id);
	}
	public void modifyCol(Col col) {
		colRepository.update(col);
	}
	/**
	 * 添加news需要判断是否已满
	 * 
	 * 添加图片 TODO
	 * @param news
	 * @return id
	 */
	public long addNews(News news, String suffix, File pic, String pic_path) {
		long add = 0;
		try {
			Col col = news.getCol();
			if (col == null) throw new DataNotGivenException("col null");		// 抛异常
			Col col_db = colRepository.get(col.getId());
			if (col_db == null) return 0;
			if (col_db.getCur_count() >= col_db.getMax_count()) return 0;
			
			if (pic != null) {
				String makePath = makePath();
				pic_path += makePath;
				String uploadPic = UploadUtil.uploadPic(pic, suffix, pic_path);
				news.setPic(Property.getProperty("PICURL") + makePath + "/" + uploadPic);
			}
			add = newsRepository.add(news);
			col_db.setCur_count(col_db.getCur_count() + 1);
			colRepository.update(col_db);
		} catch (DataNotGivenException e) {
			e.printStackTrace();
			throw e;
		}
		
		return add;
	}
	/**
	 * 删除需要将所属col的curcount - 1
	 * 
	 * @param id
	 */
	public void delNews(long id) {
		Col col = colRepository.get(newsRepository.get(id).getCol().getId());
		col.setCur_count(col.getCur_count() - 1);
		colRepository.update(col);
		
		newsRepository.del(id);
	}
	
	/**
	 * 添加图片
	 * @param news
	 */
	public void modifyNews(News news, String suffix, File pic, String pic_path) {
		News news_db = null;
		try {
			news_db = newsRepository.get(news.getId());
		} catch (NullPointerException e) {
			e.printStackTrace();
			return;
		}
		
		if (pic != null) {
			String makePath = makePath();
			pic_path += makePath;
			String uploadPic = UploadUtil.uploadPic(pic, suffix, pic_path);
			news_db.setPic(Property.getProperty("PICURL") + makePath + "/" + uploadPic);
		}
		String brief = news.getBrief();
		if (brief != null) news_db.setBrief(brief);
		String detail = news.getDetail();
		if (detail != null) news_db.setDetail(detail);
		String title = news.getTitle();
		if (title != null) news_db.setTitle(title);
		String url = news.getUrl();
		if (url != null) news_db.setUrl(url);
		newsRepository.update(news_db);
	}
	
	public Response uploadMultiPics(File[] file, String[] fileFileName, String path) {
		Response response = makeResponse();
		if (file == null) throw new DataNotGivenException("Pic null");
		String date = makePath();
		path += date;
		
		String[] paths = new String[file.length];
		for (int i = 0; i < file.length; i++) {
			paths[i] = UploadUtil.uploadPic(file[i], fileFileName[i], path);
//			paths[i] = "http://localhost:8080/yzx/" + date + "/" + paths[i];	// TODO 临时域名
			paths[i] = Property.getProperty("PICURL") + date + "/" + paths[i];	// TODO 临时域名
		}
		return response.success(paths);
	}
	
	private String makePath() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String format = sdf.format(date);
		return format;
	}
	
	private ColRepository colRepository;
	private NewsRepository newsRepository;
	@Autowired
	public void setColRepository(ColRepository colRepository) {
		this.colRepository = colRepository;
	}
	@Autowired
	public void setNewsRepository(NewsRepository newsRepository) {
		this.newsRepository = newsRepository;
	}
	
}
